04. Outer Loop - Altitude

Outer Loop Controller


The outer loop controller will be built in 2 steps, first we will build the altitude controller and then we will build the lateral position controller. Each of these can be run and tested separately to make the building and tuning processes easier.

Altitude Controller

In this first step, we will be building an altitude controller to the level of commanding velocities. Later on, we will continue to develop the altitude controller to compute thrust itself, but for now we will stop at vertical velocity as the Crazyflie allows us to control this directly.

Much like in the project, we will create a PID controller on the altitude to command velocity, so the resulting command should look like the first step of your altitude controller from the controls project:

def altitude_controller(self, alt_cmd, alt, hdot_cmd=0.0):
    hdot_cmd += self._kp_alt * (alt_cmd - alt)  # compute desired vertical velocity from altitude error
    hdot_cmd = np.clip(hdot_cmd, -self._hdot_max, self._hdot_max)  # saturate as desired
    return hdot_cmd

NOTE: for the Crazyflie a simple P controller is all that will be necessary, however try adding the I and D terms and see how it changes the controller!

And that's it! Now choose a gain (see next section "Choosing Initial Gains") and see what happens!

Once you have decided on a gain, you can run this outer loop controller using the velocity_flyer.py script as follows:

python velocity_flyer.py --uri radio://0/80/2M

Where the uri passed in should be the uri you configured for your Crazyflie.

NOTE: if you leave the lateral position gain (self._kp_pos) set to 0, then only the altitude controller will run, allowing you to focus tuning the altitude controller alone. The Crazyflie may drift slowly as it is only trying to maintain 0 lateral velocity, not hold a position, but the drift should be fairly slow.

IMPORTANT NOTE: if at any time you need to stop the script and control of the Crazyflie, if ctrl+c does not work, simply unplug the Crazyradio dongle from your computer and the Crazyflie will go into "stop" mode after a couple seconds (this will make it fall out of the sky).